<?php
namespace App\Http\Controllers\Api;

use App\V3\V3_1\Models\AdminModel;
use App\V3\V3_1\Models\DispatchBillDriverModel;
use App\V3\V3_1\Models\DispatchBillGoodsDetailModel;
use App\V3\V3_1\Models\DispatchBillModel;
use App\V3\V3_1\Models\SalesReturnGoodsDetailModel;
use App\V3\V3_1\Models\SalesReturnModel;
use App\V3\V3_1\Models\SellDriverModel;
use App\V3\V3_1\Models\SellGoodsDetail;
use App\V3\V3_1\Models\SellModel;
use App\V3\V3_1\Models\StorageManageModel;
use App\V3\V3_1\Models\SupplierCustomerModel;
use Illuminate\Routing\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

/**
 * 详情控制器
 * Class DetailController
 * @package App\Http\Controllers\Api
 */
class DetailController extends Controller
{
    /**
     * 销售详情
     * change 在 2021/11/3 10:09 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function sellFind(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' =>'required|integer',//销售单主键id
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        $model = SellModel::find($data['id']);
        if($model){
            $model['username'] = AdminModel::where('id',$model['user_id'])->value('username');
            $model['compony_name'] = SupplierCustomerModel::where(['name'=>$model['customer_name'],'main_id'=>$model['main_id']])->value('compony_name');
            $model['goods_info'] = SellGoodsDetail::where('sell_id',$model['id'])->get();
            foreach ($model['goods_info'] as &$val){
                $val['in_storage_num'] = StorageManageModel::where(['materiel_name'=>$val['name'],'type_name'=>$val['type_name'],'main_id'=>$model['main_id']])->value('in_storage_num');
            }
            $model['driver'] = SellDriverModel::where('sell_id',$model['id'])->first();
        }
        return success($model);
    }
    /**
     * 退货详情
     * change 在 2021/11/3 10:09 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function salesReturnFind(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' =>'required|integer',//销售单主键id
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        $model = SalesReturnModel::find($data['id']);
        if($model){
            $model['username'] = AdminModel::where('id',$model['user_id'])->value('username');
            $model['goods_info'] = SalesReturnGoodsDetailModel::where('return_id',$model['id'])->get();
            foreach ($model['goods_info'] as &$val){
                $val['in_storage_num'] = StorageManageModel::where(['materiel_name'=>$val['name'],'type_name'=>$val['type_name'],'main_id'=>$model['main_id']])->value('in_storage_num');
            }
        }
        return success($model);
    }
    /**
     * 客户发货单详情
     * change 在 2021/11/8 10:44 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function dispatchBillFind(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' =>'required|integer',//销售单主键id
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        $model = DispatchBillModel::find($data['id']);
        if($model){
            $model['goods_info'] = DispatchBillGoodsDetailModel::where('dispatch_bill_id',$model['id'])->get();
            $model['driver'] = DispatchBillDriverModel::where('dispatch_bill_id',$model['id'])->first();
        }
        return ($model);
    }
}
